﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Data.Model;
using ZLCMS.Business.Shop.Commands.Order;
using ZLCMS.Business.Util;
using NHibernate;
using DotNet.Common.NHibernateUtil;
using ZLCMS.Web.SystemManage.Basics.ProductSKU;
using Newtonsoft.Json;
using DotNet.Common.Command;
using ZLCMS.Web.Wap.Code;
using ZLCMS.Business.Shop.Data;



namespace ZLCMS.Web.MemberUser
{
    public partial class MyOrder : WebMemberPageBase
    {
        public int allOrderCount;
        public int unpaidCount;
       // public int unConfirmedCount;
        public int sendedCount;
        public int finishedCountCount;
        public string _type;
        public MemberInfo _memberDb;
        protected void Page_Load(object sender, EventArgs e)
        {
            _memberDb = Master.LoadMember();
          
            _type = string.IsNullOrEmpty(Request["type"]) ? "all" : Request["type"];
            
            string orderNo=Request["orderno"];
           

rptOrderBind();

            #region MyRegion
            //if (!IsPostBack)
            //{
            //    ParamKit.Bind("OrderPayStatus", DropDownListPayStatus, null, new List<ListItem>() { ParamKit.ParamItemEx_QXZ() }, null, Request["p"], "-1");
            //    ParamKit.Bind("OrderShippingStatus", DropDownListShipStatus, null, new List<ListItem>() { ParamKit.ParamItemEx_QXZ() }, null, Request["s"], "-1");
            //    //ParamKit.Bind("OrderIsConfirmed", DropDownListIsConfirm, null, new List<ListItem>() { ParamKit.ParamItemEx_QXZ() }, null, "-1", "-1");
            //    //ParamKit.Bind("OrderIsFinish", DropDownListIsFinished, null, new List<ListItem>() { ParamKit.ParamItemEx_QXZ() }, null, "-1", "-1");
            //    //ParamKit.Bind("OrderIsCancel", DropDownListIsCancel, null, new List<ListItem>() { ParamKit.ParamItemEx_QXZ() }, null, "-1", "-1");

            //    BindRepeater();
            //}
            #endregion
        }
        #region
        //protected void BindRepeater()
        //{
        //    LoadOrderListParameter param = new LoadOrderListParameter();

        //    param.PageIndex = AspNetPager1.CurrentPageIndex;
        //    param.PageSize = AspNetPager1.PageSize;
        //    param.IsCancelledFilterValue = "-1";
        //    param.IsConfirmedFilterValue = "-1";
        //    //param.IsCancelledFilterValue = DropDownListIsCancel.SelectedValue;
        //    //param.IsConfirmedFilterValue = DropDownListIsConfirm.SelectedValue;
        //    param.UserCode = _memberDb.AccountId;
        //    param.PayStatusFilterValue = DropDownListPayStatus.SelectedValue;
        //    param.ShipStatusFilterValue = DropDownListShipStatus.SelectedValue;

        //    LoadOrderListCommand cmd = new LoadOrderListCommand();
        //    LoadOrderListResult result = cmd.Execute(param) as LoadOrderListResult;

        //    if (!result.Error)
        //    {
        //        AspNetPager1.RecordCount = result.TotalCount;
        //        rp_list.DataSource = result.Result;
        //        rp_list.DataBind();
        //    }
        //}

        //protected void ButtonQuery_Click(object sender, EventArgs e)
        //{
        //    BindRepeater();
        //}

        //protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        //{
        //    BindRepeater();
        //}

        //protected string GetPayHtml(object item)
        //{
        //    ShopOrderInfo data = (ShopOrderInfo)item;

        //    if (data.PayStatus == "0")
        //    {
        //        return "<a href=\"/PayChannal/Jump2PayPage.aspx?o=" + data.OrderNo + "\">支付</a>&nbsp;|&nbsp;";
        //    }
        //    return null;
        #endregion

        #region 绑定订单列表 + private void rptOrderBind()
        /// <summary>
        /// 绑定订单列表
        /// </summary>
        private void rptOrderBind()
        {
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                try
                {
                    LoadOrderListParameter param = new LoadOrderListParameter();
                    param.MemberAccount = Session[GlobalSessionString.CurrentMemberAccount].ToString();
                    param.IsPcPay = "1";
                    IList<ShopOrderInfo> unPaidRes = new List<ShopOrderInfo>();
                        param.IsCancelledFilterValue = "0";
                        param.IsConfirmedFilterValue = "0";
                        param.PayStatusFilterValue = "0";
                        param.QueryFilterValue = "unpaid";
                        unPaidRes = (List<ShopOrderInfo>)new LoadOrderList().Execute(param).Result.List;
                        unpaidCount = unPaidRes.Count;

                        IList<ShopOrderInfo> sendedRes = new List<ShopOrderInfo>();
                        param.IsCancelledFilterValue = "0";
                        param.IsConfirmedFilterValue = "1";
                        param.ShipStatusFilterValue = "1";
                      //  param.PayStatusFilterValue = "1";
                        param.IsFinishedFilterValue = "0";
                        param.QueryFilterValue = "sended";
                        sendedRes = new LoadOrderList().Execute(param).Result.List;
                     
                        sendedCount = sendedRes.Count;

                        IList<ShopOrderInfo> finishedRes = new List<ShopOrderInfo>();
                        param.IsCancelledFilterValue = "0";
                        param.IsConfirmedFilterValue = "1";
                        param.PayStatusFilterValue = "1";
                        param.ShipStatusFilterValue = "1";
                        param.IsFinishedFilterValue = "1";
                        param.QueryFilterValue = "finished";
                        finishedRes = new LoadOrderList().Execute(param).Result.List;
                        finishedCountCount = finishedRes.Count;

                        IList<ShopOrderInfo> allRes = new List<ShopOrderInfo>();
                        MemberCategory1.MenuIndex = "qbdd";
                        param.IsCancelledFilterValue = "0";
                        param.IsConfirmedFilterValue = "";
                        param.PayStatusFilterValue = "";
                        param.IsFinishedFilterValue = "";
                        param.ShipStatusFilterValue = "";
                        allRes = new LoadOrderList().Execute(param).Result.List;
                        allOrderCount = allRes.Count;

                    if (_type == "unpaid")
                    {
                        MemberCategory1.MenuIndex = "dzfdd";
                            rpt_order.DataSource = unPaidRes;
                    }
                    //待收货
                    else if (_type == "sended")
                    {
                        rpt_order.DataSource = sendedRes;
                    }
                    //已结束
                    else if (_type == "finished")
                    {
                          rpt_order.DataSource = finishedRes;
                    }
                     //已结束
                    else if (_type == "all")
                    {
                        rpt_order.DataSource =allRes;
                    }
                    rpt_order.DataBind();
                }
                catch (Exception ex)
                {
                    Response.Write("查询出错!" + ex.Message);
                }
            }
        }
        #endregion

        #region 绑定待付款订单的产品信息 + protected void rptOrder1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        /// <summary>
        /// 绑定待付款订单的产品信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void rptOrder_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            //判断里层repeater处于外层repeater的哪个位置（ AlternatingItemTemplate，FooterTemplate，

            //HeaderTemplate，，ItemTemplate，SeparatorTemplate）
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Repeater rep = e.Item.FindControl("rpt_pro") as Repeater;//找到里层的repeater对象
                ShopOrderInfo order = (ShopOrderInfo)e.Item.DataItem;//找到分类Repeater关联的数据项 

                //LoadOrderDetailResult res = (LoadOrderDetailResult)new LoadOrderDetailCommand().Execute(new LoadOrderDetailParameter() { OrderNo = order.OrderNo });

                //rep.DataSource = res.Result;
                //rep.DataBind();
            }
        }
        #endregion
        public string GetSkuItemHtml(string skuProductNo)
        {
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                ShopSkuProduct product = session.CreateQuery("from ShopSkuProduct where ProductSkuCode=:code ").SetParameter("code", skuProductNo).UniqueResult<ShopSkuProduct>();
                if (product==null)
                {
                    return "";
                }
                List<SKUGroup> grouplist = JsonConvert.DeserializeObject<List<SKUGroup>>(product.ParamCode);
                string itemList = "";
                foreach (SKUGroup cd in grouplist)
                {
                    itemList += "<span>[" + cd.ItemName + "]</span> ";
                }

                return itemList;
            }
        }

        public string GetPaymentName(string id)
        {
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                ShopPaymentInfo payment = session.CreateQuery("from ShopPaymentInfo where Id=:id ").SetParameter("id", id).UniqueResult<ShopPaymentInfo>();

                return payment == null ? "" : payment.PaymentName ;
            }
        }

    }
}
           

      
    
